package com.goods.controller.business;

import com.goods.common.model.business.ProductCategory;
import com.goods.common.response.ResponseBean;
import com.goods.common.vo.business.ProductCategoryTreeNodeVO;
import com.goods.common.vo.system.PageVO;
import com.goods.service.ProductCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;
import java.util.List;


@RestController
@RequestMapping("/business/productCategory")
public class ProductCategoryController {

    @Autowired
    private ProductCategoryService productCategoryService;

    //加载分类数据
    @GetMapping("categoryTree")
    public ResponseBean getCategoryTree(@RequestParam(value = "pageNum") Integer pageNum,
                                        @RequestParam(value = "pageSize") Integer pageSize) {
        PageVO productList = productCategoryService.getCategoryTree(pageNum, pageSize);
        return ResponseBean.success(productList);
    }

    //加载父分类
    @GetMapping("getParentCategoryTree")
    public ResponseBean getParentCategoryTree() {
        List<ProductCategoryTreeNodeVO> productCategoryList = productCategoryService.getParentCategoryTree();
        return ResponseBean.success(productCategoryList);
    }

    //添加
    @PostMapping("add")
    public ResponseBean add(@RequestBody ProductCategory productCategory) {
        productCategoryService.add(productCategory);
        return ResponseBean.success();
    }

    //编辑
    @GetMapping("edit/{id}")
    public ResponseBean edit(@PathVariable Long id) {
        ProductCategory productCategory = productCategoryService.edit(id);
        return ResponseBean.success(productCategory);
    }

    //修改
    @PutMapping("update/{id}")
    public ResponseBean update(@PathVariable Long id , @RequestBody ProductCategory productCategory) {
        productCategoryService.update(id,productCategory);
        return ResponseBean.success();
    }

    //删除,有子分类的、和物资引用的分类不能删除
    @DeleteMapping("delete/{id}")
    public ResponseBean delete(@PathVariable Long id) {
        boolean flag = productCategoryService.delete(id);
        if (flag) {
            return ResponseBean.success();
        }
        return ResponseBean.error("有子分类的、和物资引用的分类不能删除");
    }
}
